﻿using Application.Utils;
using Infrastructure.Utils;
using Infrastructure.WebApi;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Neoit.Utils.Extensions;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using Web.Middlewares;
using static Infrastructure.Extension.EnumHelper;

namespace Web.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InfoController : ControllerBase
    {
        private readonly IHttpContextAccessor _contextAccessor;
        private readonly TokenOptions _tokenSetting;
        private IConfiguration _config { get; set; }
        private JobManagerDbContext _db { get; set; }

        public InfoController(IConfiguration config, IOptions<TokenOptions> tokenSetting, IHttpContextAccessor contextAccessor, JobManagerDbContext db)
        {
            _config = config;
            _tokenSetting = tokenSetting.Value;
            _contextAccessor = contextAccessor;
            _db = db;
        }
        /// <summary>
        /// 测试模型绑定
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost, AllowAnonymous]
        public ResponseBase<WebApiTest> CheckModelBind([FromBody] WebApiTest req)
        {
            return Result.Success(req);
        }
        /// <summary>
        /// 系统信息
        /// </summary>
        /// <returns></returns>
        [HttpGet, AllowAnonymous]
        public ResponseBase<object> System()
        {
            var res = new
            {
                Name = "SystemMange",
                ASPNETCORE_ENVIRONMENT = EnvironmentVariableHelper.NetCoreEnvironment,
                DateTimeNowFormat = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"),
                DateTimeNow = DateTime.Now,
                COMMITID = _config["COMMITID"],
                RemoteIpAddressIpv6 = _contextAccessor.HttpContext.Connection.RemoteIpAddress.ToString(),
                RemoteIpAddressIpv4 = _contextAccessor.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString(),
                X_Real_IP = _contextAccessor.HttpContext.Request.Headers["X-Real-IP"].ToString(),
                X_Forwarded_For = _contextAccessor.HttpContext.Request.Headers["X-Forwarded-For"].ToString(),
                RealIp = _contextAccessor.HttpContext.RealIP(),
                RealIp2 = Request.HttpContext.RealIP(),
                Tag = "315",
                tokenSetting = new
                {
                    _tokenSetting.access_token_minute,
                    _tokenSetting.refresh_token_minute,
                    _tokenSetting.refresh_token_limit_minute,
                }

            };
            return Result.Success<object>(res);
        }
        /// <summary>
        /// 枚举信息
        /// </summary>
        /// <returns></returns>
        [HttpGet, AnonymousRole]
        [ResponseCache(Duration = 10)]
        public Dictionary<string, List<EnumInfo>> GetEnums()
        {
            var enumList = new Dictionary<string, List<EnumInfo>>();
            var enums = typeof(JobStatus).Assembly.GetTypes()
                .Where(t => t.IsEnum);

            foreach (var enumType in enums)
            {
                var enumInfos = enumType.GetFields(BindingFlags.Public | BindingFlags.Static)
                    .Where(f => f.IsLiteral)
                    .Select(f => new EnumInfo
                    {
                        Value = (int)f.GetValue(null),
                        DisplayName = GetDisplayName(f)
                    })
                    .ToList();

                enumList.Add(enumType.Name.FirstToLower(), enumInfos);
            }
            return enumList;
            string GetDisplayName(FieldInfo fieldInfo)
            {
                var displayNameAttribute = fieldInfo.GetCustomAttribute<DisplayAttribute>();
                if (displayNameAttribute != null)
                {
                    return displayNameAttribute.Name;
                }
                return fieldInfo.Name;
            }
        }
    }
}
